package com.heking.zplayer.qlyopen;

import android.os.Handler;
import android.os.Looper;

/**
 * @Author: zhoug
 * @Date: 2024-11-11
 * @Description:
 */
public class UpdateTimeHelper {
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private int mInterval = 1000;//间隔时间
    private TimeBarView mTimeBarView;
    private QlyPlayer mPlayer;
    private OnUpdateTimeListener mOnUpdateTimeListener;

    public UpdateTimeHelper() {
    }

    public UpdateTimeHelper(TimeBarView mTimeBarView, QlyPlayer mPlayer) {
        this.mTimeBarView = mTimeBarView;
        this.mPlayer = mPlayer;
    }

    public void setonUpdateTimeListener(OnUpdateTimeListener onUpdateTimeListener) {
        this.mOnUpdateTimeListener = onUpdateTimeListener;
    }

    public void setInterval(int interval) {
        this.mInterval = interval;
    }


    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (mPlayer != null) {
                long currentTime = mPlayer.getCurrentTime();
                if (currentTime > 0) {
                    if (mTimeBarView != null) {
                        mTimeBarView.setCurrentTime(currentTime);
                    }
                    if (mOnUpdateTimeListener != null) {
                        mOnUpdateTimeListener.onUpdateTime(currentTime);
                    }
                }
                mHandler.postDelayed(runnable, mInterval);
            }
        }
    };


    public void start() {
        stop();
        mHandler.postDelayed(runnable, mInterval);
    }

    public void stop() {
        mHandler.removeCallbacks(runnable);
    }


    public interface OnUpdateTimeListener {
        void onUpdateTime(long currentTime);
    }

}
